package com.icss.mqsp.util;

import com.icss.mqsp.model.ReportDefine;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AnalyseJsonUtil
{
  private static final String BASTPATH = "config/report";
  
  public static JSONObject getPageJson(String pid)
  {
    BufferedReader reader = null;
    String laststr = "";
    try
    {
      reader = new BufferedReader(ResourcesUtil.getResourceAsReader("config/report/" + pid + ".json"));
      String tempString = null;
      while ((tempString = reader.readLine()) != null) {
        laststr = laststr + tempString;
      }
      reader.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
      if (reader != null) {
        try
        {
          reader.close();
        }
        catch (IOException s)
        {
          s.printStackTrace();
        }
      }
    }
    finally
    {
      if (reader != null) {
        try
        {
          reader.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
    JSONObject js = JSONObject.fromObject(laststr);
    return js;
  }
  
  public static List<ReportDefine> getDefine(String pid)
  {
    List<ReportDefine> reportDefineList = new ArrayList();
    JSONObject json = getPageJson(pid);
    JSONArray jsArray = json.getJSONArray("reports");
    for (int i = 0; i < jsArray.size(); i++)
    {
      JSONObject js = jsArray.getJSONObject(i);
      ReportDefine rd = new ReportDefine();
      rd.setPid(pid);
      rd.setDataSource(js.getString("datasource"));
      if (rd.getDataSource().equals("SQL"))
      {
        rd.setId(js.getString("id"));
      }
      else
      {
        rd.setId(js.getString("id"));
        rd.setAppid(js.getString("appid"));
        rd.setTitle(js.getString("title"));
        
        String buildType = js.getString("buildType");
        rd.setBuildType(buildType);
        if (buildType.equals("CHART"))
        {
          String subType = js.getString("subType");
          rd.setSubType(subType);
          if (subType.equals("line-bar")) {
            rd.setCharts(js.getJSONArray("charts"));
          }
          if (subType.equals("bar-line")) {
            rd.setCharts(js.getJSONArray("charts"));
          }
          if (subType.equals("vertcal")) {
            rd.setCharts(js.getJSONArray("charts"));
          }
          if (subType.equals("layer-bar")) {
            rd.setCharts(js.getJSONArray("charts"));
          }
          if (subType.equals("single_bar")) {
            rd.setCharts(js.getJSONArray("charts"));
          }
          if (subType.equals("stack_bar")) {
            rd.setCharts(js.getJSONArray("charts"));
          }
          if (subType.equals("line")) {
            rd.setCharts(js.getJSONArray("charts"));
          }
        }
      }
      reportDefineList.add(rd);
    }
    return reportDefineList;
  }
}
